package com.ersin.jdbc;

import java.sql.*;

public class Connect {

  // MySQL icin hazirlanmis dosya (mysql dizininde) 
  String mySQLdriver = "com.mysql.jdbc.Driver";
  String URL = "jdbc:mysql://localhost:3306/testdb";
  
  //String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  //String URL = "jdbc:odbc:japdb";

  String username = "root";
  String password = "12345";
  Connection japConnection = null;

  public Connect() {
    prepare(mySQLdriver, URL, username, password);
  }

  public Connect(String driver, String URL) {
    prepare(driver, URL, null, null);
  }

  public Connect(String driver, String URL, String user, String pass) {
    prepare(driver, URL, user, pass);
  }

  public void prepare(String driver, String URL, String user, String pass) {
    try {
      Class.forName(driver);
    }
    catch (Exception e) {
      System.out.println("JDBC surucusu yuklenemedi.");
    }
    username = user;
    password = pass;
    this.URL = URL;
  }

  public Connection connect() {
    try {
      if (username == null) {
        japConnection =
            DriverManager.getConnection(URL);
      }
      else {
        japConnection = DriverManager.getConnection(URL, username,
            password);
      }
    }
    catch (Exception e) {
      System.out.println("Baglanti kurulamadi. " + e);
      System.exit(0);
    }
    return japConnection;
  }

  public void disconnect() {
    try {
      japConnection.close();
    }
    catch (Exception e) {
      System.out.println("Baglanti kapatilamadi.");
    }
  }
}